package com.guoqing.example.controller;

import com.guoqing.example.model.MailModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Author zhangguoqing
 * @Date 2020/8/28
 */
@RestController
public class MailController {
    @Autowired
    private MailSender mailSender;
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 例子 example
     */
    @RequestMapping("/sendMail")
    public void sendMail() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 发送方,和配置文件中的的username相同
        mailMessage.setFrom(from);
        // 收件人
        mailMessage.setTo("964246330@qq.com");
        // 标题
        mailMessage.setSubject("测试发邮件");
        // 正文
        mailMessage.setText("你好，我正在测试发邮件功能,请忽略这条消息");
        //发送
        mailSender.send(mailMessage);
    }

    @PostMapping("/sendMailForModel")
    public void sendMailForModel(MailModel model) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 发送方,和配置文件中的的username相同
        mailMessage.setFrom(from);
        // 收件人
        mailMessage.setTo(model.getTo());
        // 标题
        mailMessage.setSubject(model.getSubject());
        // 正文
        mailMessage.setText(model.getText());
        //发送
        mailSender.send(mailMessage);
    }

    @PostMapping("/sendMailHTML")
    public void sendMailHTML(MailModel model) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(model.getTo());
            mimeMessageHelper.setSubject(model.getSubject());
            mimeMessageHelper.setText(model.getText(), true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/sendMailAttachment")
    public void sendMailAttachment(MailModel model) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(model.getTo());
            mimeMessageHelper.setSubject(model.getSubject());
            mimeMessageHelper.setText(model.getText(), true);
            //附件
            FileSystemResource file = new FileSystemResource("E:\\user.xlsx");
            mimeMessageHelper.addAttachment("文件名称.xlsx", file);
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
